package com.briup.estore.Service;

import com.briup.estore.bean.Shopcar;
import com.briup.estore.mapper.ShopCarMapper;
import com.briup.estore.vo.ShopCarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class ShopCarService {
    @Autowired
    private ShopCarMapper shopCarMapper;
    public void addCar(int userId, int bookId) {
        //sql语句
        //insert into es_shopcar(user_id,book_id,num) value(1,12,1);
        // 根据user_id 和book_id 去数据库查询记录  查不到记录
        Shopcar shopcarFromDB=shopCarMapper.findByUserIdAndBookId(userId,bookId);
        if (shopcarFromDB==null){
            Shopcar shopcar = new Shopcar();
            shopcar.setUserId(userId).setBookId(bookId).setNum(1);;
            //第一次添加 num=1 insert
            shopCarMapper.addCar(shopcar);
        }else{
            //如果不是 在num的基础上+1 update
            shopcarFromDB.setNum(shopcarFromDB.getNum()+1);
            shopCarMapper.updateCar(shopcarFromDB);
        }
    }

    public List<ShopCarVO> listCar(int userId) {
//        调用mapper查询购物车的书
        return shopCarMapper.listCar(userId);
    }
}
